SRCDIR := src
OBJDIR := obj
EXECUTABLE_SERVER := server
EXECUTABLE_CLIENT := client
SOURCES := $(wildcard $(SRCDIR)/*.c)
OBJECTS := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SOURCES))

SOURCES_SERVER :=  $(addprefix$(EXECUTABLE_CLIENT),.c)
OBJECTS_SERVER := $(patsubst %.c, %.o, $(SOURCES_SERVER))

SOURCES_CLIENT := $(OBJECTS) $(addprefix$(EXECUTABLE_CLIENT),.c)
OBJECTS_CLIENT := $(patsubst %.c, %.o, $(SOURCES_CLIENT))

CC:=gcc
CFLAGS:= -Wall -Iinc -pthread -lmysqlclient -lssl -lcrypto -ll8w8jwt

$(shell mkdir -p $(OBJDIR))

$(EXECUTABLE_SERVER):$(OBJECTS)
	$(CC) $^ server.c -o $@ $(CFLAGS)

$(EXECUTABLE_CLIENT):$(OBJECTS)
	$(CC) $^ client.c -o $@ $(CFLAGS)


$(OBJDIR)/%.o:$(SRCDIR)/%.c
	$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean
clean:
	rm -rf $(OBJDIR) $(EXECUTABLE_CLIENT) $(EXECUTABLE_SERVER)

.PHONY: rebuild
rebuild:clean all_server

.PHONY: rebuild_server
rebuild:clean all_server

.PHONY: rebuild_client
rebuild:clean all_client

.PHONY: all_server
all_server: create_dirs $(EXECUTABLE_SERVER)

.PHONY: all_client
rebuild_client: create_dirs $(EXECUTABLE_CLIENT)

.PHONY:create_dirs
create_dirs:
	mkdir -p $(OBJDIR)

.SILENT:
